//
//  LHCDetailsViewController.m
//  MyAddressBook
//
//  Created by lihongcheng on 16/4/22.
//  Copyright © 2016年 lihongcheng. All rights reserved.
//

#import "LHCDetailsViewController.h"
#import "LHCUpdateViewController.h"

@interface LHCDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) UITableView *tableView;
//@property (nonnull,strong) NSMutableArray *dataArray;

@end

@implementation LHCDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //self.dataArray = [[NSMutableArray alloc] init];
    [self createMainView];
    
}

-(void)createMainView
{
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:
                                 UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick:)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    self.navigationItem.title = @"通讯录";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *strID  = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        cell.textLabel.text = [NSString stringWithFormat:@"姓名: %@",self.model.name];
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = [NSString stringWithFormat:@"手机: %@",self.model.phone];
    }
    if (indexPath.row == 2) {
        cell.textLabel.text = [NSString stringWithFormat:@"年龄: %@",self.model.age];
    }
    if (indexPath.row == 3) {
        cell.textLabel.text = [NSString stringWithFormat:@"邮箱: %@",self.model.email];
    }
    if (indexPath.row == 4) {
        cell.textLabel.text = [NSString stringWithFormat:@"地址: %@",self.model.address];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 80;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 80)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(50, 30, ScreenWidth-100, 40);
    deleteBtn.backgroundColor = [UIColor orangeColor];
    deleteBtn.layer.cornerRadius = 5;
    deleteBtn.layer.masksToBounds = YES;
    [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:deleteBtn];
    return view;
}

-(void)deleteBtnClick:(UIButton *)btn
{
    BOOL b = [LHCSqlOperate deleteDataFromSql:self.model];
    if (b) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"删除失败" message:@"请再试一次" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alter show];
    }
}

-(void)rightBtnClick:(UIBarButtonItem *)btn
{
    LHCUpdateViewController *uvc = [[LHCUpdateViewController alloc] init];
    uvc.model = self.model;
    [self.navigationController pushViewController:uvc animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
